package net.csdn.www;

import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;

import com.sun.jmx.snmp.tasks.Task;

public class ImageSave {
	
	private static long count = 0;

	public static void saveImg(String urlStr,String filePath) {
		count++;
		try {
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			String dateStr = count + "." + dateFormat.format(now) ;
			filePath = filePath + File.separator + dateStr + ".jpg";
			File file = new File(filePath);
			if (file.exists()) {
				return;
			}
			
			urlStr = urlStr.trim();
			URL url = new URL(urlStr);
			BufferedImage image = ImageIO.read(url);
			ImageIO.write(image, "JPG", file);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static String getImgName(String url) {
		String[] strs = url.split("/");
		int size = strs.length;
		//return dateStr + ;
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String dateStr = count + "." + dateFormat.format(now) ;
		count ++;
		return dateStr + "_" + strs[size - 1];
	}

}
